﻿/*
swfUpload v0.2
Copyright (c) 2011 wangli
*/

function upFileList(flashID,upLoadItem,src,postUrl){
	this.upfile=$(upLoadItem);
	this.flashID=$(flashID);
	//this.flashID.html(this.appendFlash("UPLoad",222,222,src,"#FFFFFF",10));
	this.fileArr=new Array();
	this.fileNameArr=new Array();
	this.filePathArr=new Array();
	this.flash=getFlashMovieObject(flashID);
	this.url=postUrl;
	this.postCon={};
	this.attr={total:2,size:500,types:"Images (*.jpg, *.gif, *.png):*.jpg;*.gif;*.png"};
}
upFileList.prototype.postParameters=function(){
	return this.postCon;
}
upFileList.prototype.postUrl=function(){
	return this.url;
}
upFileList.prototype.getAttr=function(){
	return this.attr;
}
upFileList.prototype.alert=function(val){
	alert(val);
}
//创建每个上传文件
upFileList.prototype.createFile = function (count,fileName) {
	file=new FileProgress("file_"+count,fileName);
	file.count=count;
	file.flash=this.flash;
	file.toggleCancel();
  	this.upfile.append(file.content);
	this.fileArr.push(file);
	this.fileNameArr.push(fileName);
	
	var ios=this;
	//覆盖删除文件操作
	file.deleteFile=function(){
		ios.fileArr.splice(this.count,1);
		ios.fileNameArr.splice(this.count,1);
		ios.filePathArr.splice(this.count,1);
		var lg=ios.fileArr.length;
		for(var i=0;i<lg;i++){
			ios.fileArr[i].count=i;
		}
		ios.deleteDataComplete();
	}
};
upFileList.prototype.fileError=function(count,val){
	this.fileArr[count].fileError(val);
	this.filePathArr.push("失败");
}
upFileList.prototype.fileSizeError=function(count,val){
	this.fileArr[count].sizeError(val);
	this.filePathArr.push("失败");
}
upFileList.prototype.fileSetProgress=function(count,val){
	this.fileArr[count].setProgress(parseInt(val));
}
upFileList.prototype.completeHandler=function(count,size,filePath){
	this.fileArr[count].setComplete(size);
	this.filePathArr.push(filePath);
}
//上传完成，由flash组件控制
upFileList.prototype.completeExport=function(){}
//取消并删除文件完成后
upFileList.prototype.deleteDataComplete=function(){}


//设置flash对象
upFileList.prototype.appendFlash=function(id,w,h,scr,bgcolor,vs){
	var str="<object height=\""+h+"\" width=\""+w+"\" id=\""+id+"\" codebase=\"http://active.macromedia.com/flash2/cabs/swflash.cab#version="+vs+",0,0,0\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"><param value=\""+scr+"\" name=\"movie\"><param value=\"high\" name=\"quality\"><param value=\"false\" name=\"play\"><param value=\""+bgcolor+"\" name=\"bgcolor\"><embed height=\""+h+"\" width=\""+w+"\" pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" bgcolor=\""+bgcolor+"\" quality=\"high\" src=\""+scr+"\" name=\""+id+"\" swliveconnect=\"true\" play=\"false\"></object>"
	return str;
}
//创建每个文件的进度条方法
function FileProgress(ID,con) {
	
	//创建一个加载进度信息容器
	var content = document.createElement("div");
	content.className = "progressWrapper";
	content.id = ID;
  
    //进度条内部容器
	var fileProgressElement = document.createElement("div");
	fileProgressElement.className = "progressContainer";
	//创建取消传送按钮
	var progressCancel = document.createElement("a");
	progressCancel.className = "progressCancel";
	progressCancel.href = "javascript:void(0)";
	progressCancel.appendChild(document.createTextNode("删除"));
	//创建除送内容提示信息
	var progressText = document.createElement("div");
	progressText.className = "progressName";
	progressText.appendChild(document.createTextNode(con));
	//创建加载进度条
	var progressBar = document.createElement("div");
	progressBar.className = "progressBarInProgress";
	//创建加载进度条进度填充
	var progressStatus = document.createElement("div");
	progressStatus.className = "progressBarStatus";
	progressStatus.innerHTML = "&nbsp;";
	progressBar.appendChild(progressStatus);
	//将内容个添加到容器
	fileProgressElement.appendChild(progressText);
	fileProgressElement.appendChild(progressBar);
	fileProgressElement.appendChild(progressCancel);
	content.appendChild(fileProgressElement);
	
	this.count=0;
	this.content=content;
	this.conID=ID;
	this.fileProgressElement=fileProgressElement;
	this.progressCancel=progressCancel;
	this.progressText=progressText;
	this.progressBar=progressBar;
	this.progressStatus=progressStatus;
}
//文件传输错误
FileProgress.prototype.fileError = function (val) {
	this.fileProgressElement.className = "progressContainer red";
	$(this.progressText).append("&nbsp;&nbsp;"+val);
	$(this.progressBar).remove();
};
//文件大小不符合
FileProgress.prototype.sizeError = function (bytesTotal) {
	this.fileProgressElement.className = "progressContainer red";
	$(this.progressText).append("&nbsp;&nbsp;("+bytesTotal+"KB&nbsp;) 文件太大了");
	$(this.progressBar).remove();
};
//取消上传
FileProgress.prototype.toggleCancel = function () {
	//this.progressCancel.style.visibility = show ? "visible" : "hidden";
	var ios=this;
	$(this.progressCancel).click(function(){
		//删除html中的上传进度条
		$(ios.content).remove();
		//停止并删除flash组件中的正在上传中的文件
		ios.flash.deleteFile(ios.count);
		//删除执行文件后更改数据
		ios.deleteFile();
		//删除服务器上的文件
		ios.deleteServerFile();
	}); 
};
//删除自身文件操作
FileProgress.prototype.deleteFile=function(){}
//删除服务器上的文件
FileProgress.prototype.deleteServerFile=function(){}
//上传进度样式更改
FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.progressStatus.style.width = percentage + "%";
};
//上传完成之后更改状态
FileProgress.prototype.setComplete = function (bytesTotal) {
	$(this.progressBar).remove();
	$(this.progressText).append("&nbsp;&nbsp;("+bytesTotal+"KB&nbsp;)");
	//$(this.progressText).append("&nbsp;&nbsp;"+bytesTotal+"KB&nbsp;");
};
//获取flash文件对象
function getFlashMovieObject(movieName)
{
	
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else
  {
    return document.getElementById(movieName);
  }
}
